package org.oy.sealogger.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.oy.sealogger.common.Config;
import org.oy.sealogger.common.Messages;
import org.oy.sealogger.logger.XMLLogger;
import org.oy.sealogger.parser.CurrentState;
import org.oy.sealogger.route.WaypointData;

import fileReader.demo.reader.Simulator;

public class ButtonPanel implements ActionListener {
	
	private Logger log = Config.getLogger();
	private Simulator simulator = null;
	private XMLLogger xmllogger = null;
	private LoggerGraphics lg = null;

	private JPanel buttonPanel = new JPanel();
	private JButton startLogging = new JButton(Messages.getMessage("button.label.start_logging"));
	private JButton addWaypoint = new JButton(Messages.getMessage("button.label.add_waypoint"));
	private JButton autoCenter = new JButton(Messages.getMessage("button.label.auto_center_on"));
	
	private boolean logging = false;
	private boolean centerBoat = true;
	
	
	public ButtonPanel(Simulator simu, LoggerGraphics l, XMLLogger xmllogger) {
		this.simulator = simu;
		this.lg = l;
		this.xmllogger = xmllogger;

		initButtons();
	}
	
	public void initButtons() {
		startLogging.addActionListener(this);
		addWaypoint.addActionListener(this);
		autoCenter.addActionListener(this);
		
		buttonPanel.add(startLogging);
		buttonPanel.add(addWaypoint);
		buttonPanel.add(autoCenter);
		
		this.simulator.getContentPane().add(buttonPanel,BorderLayout.NORTH);
		buttonPanel.setVisible(false);
		
	}
	
	public void showButtons() {
		buttonPanel.setVisible(true);
	}
	public void hideButtons() {
		buttonPanel.setVisible(false);
	}
	
	public boolean getCenterMode() {
		return this.centerBoat;
	}

	public void actionPerformed(ActionEvent e) {
		
		if (!logging && e.getSource() == startLogging) {
			if (xmllogger != null) {
				logging = true;
				xmllogger.startLogging();
				startLogging.setText(Messages.getMessage("button.label.stop_logging"));
			} else {
				System.out.println("null");
			}
		} else if (logging && e.getSource() == startLogging) {
			logging = false;
			xmllogger.stopLogging();
			startLogging.setText(Messages.getMessage("button.label.start_logging"));
		}
		
		if (centerBoat && e.getSource() == autoCenter) {
			centerBoat = false;
			autoCenter.setText(Messages.getMessage("button.label.auto_center_off"));
		} else if (!centerBoat && e.getSource() == autoCenter) {
			centerBoat = true;
			autoCenter.setText(Messages.getMessage("button.label.auto_center_on"));
		}
		
		if (logging && e.getSource() == addWaypoint) {			
			CurrentState s = CurrentState.getInstance();			
			xmllogger.logWaypoint(new WaypointData(s.getLatitude(), s.getLongitude()));
		}
	}
}
